using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace 修为尽失.辅助
{
    public static class XmlHelper
    {
        public static string Serializer<T>(T serialObject) where T : class
        {
            try
            {
                XmlSerializer ser = new XmlSerializer(typeof(T));
                MemoryStream mem = new MemoryStream();
                XmlTextWriter writer = new XmlTextWriter(mem, Encoding.UTF8);
                ser.Serialize(writer, serialObject);
                writer.Close();
 
                return Encoding.UTF8.GetString(mem.ToArray());
            }
            catch (Exception ex)
            {
                return null;
            }
        }
 
        public static T Deserialize<T>(string str) where T : class
        {
            try
            {
                XmlSerializer mySerializer = new XmlSerializer(typeof(T));
                StreamReader mem2 = new StreamReader(
                    new MemoryStream(Encoding.UTF8.GetBytes(str)),
                    Encoding.UTF8);
 
                return (T)mySerializer.Deserialize(mem2);
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}